﻿using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using log4net;
using System.Reflection;

namespace AnyOffice.general.calendar.info
{
    public partial class edit :  ModulePage
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
     
 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                ListItem item;
                int calId = (Request.QueryString["calid"] != null) ? int.Parse(Request.QueryString["calid"].ToString()) : 0;
                this.ViewState["CalId"] = calId;
                string userId = (Request.QueryString["user_id"] != null) ? Request.QueryString["user_id"].ToString() : "";
                if (!SecurityManager.IsCanManage(userId))
                {
                    base.CloseConn();
                    Response.Write(base.ShowErrorNoNav("无权限"));
                    Response.End();
                }
                DataTable table = new CalendarDao().SelectByCalId(base.Conn, calId, userId);
                if (table.Rows.Count == 0)
                {
                    this.pnlMain.Visible = false;
                    this.litMessage.Text = base.ShowPromptClose("无记录");
                    return;
                }
                DateTime time = Convert.ToDateTime(table.Rows[0]["StartTime"].ToString());
                this.ViewState["SpecifyDate"] = time.ToString("yyyy-MM-dd");
                this.litSpecifyDate.Text = time.Year.ToString() + "年" + time.Month.ToString() + "月" + time.Day.ToString() + "日";
                this.txtContent.Value = table.Rows[0]["Content"].ToString();
                this.ViewState["UserId"] = table.Rows[0]["UserId"].ToString();
                for (int i = 0; i < 24; i++)
                {
                    item = new ListItem((i.ToString().Length == 1) ? ("0" + i.ToString()) : i.ToString(), i.ToString());
                    if (int.Parse(item.Value) == DateTime.Parse(table.Rows[0]["StartTime"].ToString()).Hour)
                    {
                        item.Selected = true;
                    }
                    this.ddlStartHour.Items.Add(item);
                }
                for (int j = 0; j < 24; j++)
                {
                    item = new ListItem((j.ToString().Length == 1) ? ("0" + j.ToString()) : j.ToString(), j.ToString());
                    if (int.Parse(item.Value) == DateTime.Parse(table.Rows[0]["EndTime"].ToString()).Hour)
                    {
                        item.Selected = true;
                    }
                    this.ddlEndHour.Items.Add(item);
                }
                for (int k = 0; k < 60; k++)
                {
                    item = new ListItem((k.ToString().Length == 1) ? ("0" + k.ToString()) : k.ToString(), k.ToString());
                    if (int.Parse(item.Value) == DateTime.Parse(table.Rows[0]["StartTime"].ToString()).Minute)
                    {
                        item.Selected = true;
                    }
                    this.ddlStartMinute.Items.Add(item);
                }
                for (int m = 0; m < 60; m++)
                {
                    item = new ListItem((m.ToString().Length == 1) ? ("0" + m.ToString()) : m.ToString(), m.ToString());
                    if (int.Parse(item.Value) == DateTime.Parse(table.Rows[0]["EndTime"].ToString()).Minute)
                    {
                        item.Selected = true;
                    }
                    this.ddlEndMinute.Items.Add(item);
                }
                this.ckbMobile.Visible = SmsDao.IsMobile(SmsDao.SmsType.Calendar);
            }
            if (this.Page.IsPostBack)
            {
                this.pnlMain.Visible = false;
                int num9 = Convert.ToInt32(this.ViewState["CalId"]);
                CalendarDao dao2 = new CalendarDao();
                CalendarData data = new CalendarData();
                data.CalId = num9;
                data.CalType = 1;
                data.StartTime = DateTime.Parse(this.ViewState["SpecifyDate"].ToString() + " " + this.ddlStartHour.SelectedValue + ":" + this.ddlStartMinute.SelectedValue);
                data.EndTime = DateTime.Parse(this.ViewState["SpecifyDate"].ToString() + " " + this.ddlEndHour.SelectedValue + ":" + this.ddlEndMinute.SelectedValue);
                data.UserId = this.ViewState["UserId"].ToString();
                data.Content = this.txtContent.Value;
                data.ManagerId = base.User.Identity.Name;
                dao2.Update(base.Conn, data);
                if (this.ckbSms.Checked || this.ckbMobile.Checked)
                {
                    SmsDao dao3 = new SmsDao();
                    SmsData data2 = new SmsData();
                    data2.FromId = base.User.Identity.Name;
                    data2.ToId = this.ViewState["UserId"].ToString();
                    data2.Type = 5;
                    data2.SendTime = DateTime.Parse(this.ViewState["SpecifyDate"].ToString() + " " + this.ddlStartHour.SelectedValue + ":" + this.ddlStartMinute.SelectedValue);
                    data2.Content = "请查看日程安排!\n内容:" + this.txtContent.Value;
                    data2.RelateId = num9;
                    dao3.BizUpdate(base.Conn, data2, this.ckbSms.Checked, this.ckbMobile.Checked);
                }
                this.litMessage.Text = base.ShowPromptClose("保存成功");
                DateTime time6 = new DateTime(2000, 1, 1);
                TimeSpan span = (TimeSpan)(Convert.ToDateTime(this.ViewState["SpecifyDate"].ToString()) - time6);
                StringBuilder builder = new StringBuilder();
                builder.Append("<script Language=\"JavaScript\">");
                builder.Append("window.opener.__doPostBack('DataCalendar1','" + span.Days.ToString() + "');");
                builder.Append("</script>");
                if (!this.Page.IsClientScriptBlockRegistered("refresh"))
                {
                    this.Page.RegisterClientScriptBlock("refresh", builder.ToString());
                }
            }
        }
 

    }
}
